package com.atm.dao;

import com.atm.pojo.RunMessage;
import com.atm.utill.DBUtils;
import com.atm.utill.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RunMessageDao {
    QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
    /**
     * 添加账单信息
     * @param runMessage 账单信息
     */
    public void addRunMessage(RunMessage runMessage) {
//        Connection connection = DBUtils.getConnection();
//        PreparedStatement preparedStatement = null;
//        try {
//            preparedStatement  = connection
//                    .prepareStatement("insert into runmessage values(null,?,?,?,?,?)");
//            preparedStatement.setString(1,runMessage.getIcno());
//            preparedStatement.setString(2, runMessage.getRm_date());
//            preparedStatement.setString(3, runMessage.getRm_currency());
//            preparedStatement.setDouble(4,runMessage.getRm_balance());
//            preparedStatement.setDouble(5,runMessage.getRm_rest());
//            preparedStatement.executeUpdate();
//        }catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            DBUtils.release(connection,preparedStatement,null);
//        }
        String sql ="insert into runmessage values(null,?,?,?,?,?)";
        try {
            queryRunner.update(sql,runMessage.getIcno(),
                    runMessage.getRm_date(),
                    runMessage.getRm_currency(),
                    runMessage.getRm_balance()
                    ,runMessage.getRm_rest());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 账户记录
     * @param icno 卡号
     * @return 账户记录
     */
    public List<RunMessage> findAllDetails(String icno) {
//        Connection connection = DBUtils.getConnection();
//        PreparedStatement preparedStatement = null;
//        try {
//            preparedStatement  = connection
//                    .prepareStatement("select * from runmessage where icno = ? ");
//            preparedStatement.setString(1,icno);
//            ResultSet resultSet = preparedStatement.executeQuery();
//            ArrayList<RunMessage> runMessages = new ArrayList<>();
//            if (resultSet.next()) {
//                RunMessage runMessage = new RunMessage();
//                runMessage.setId(resultSet.getInt(1));
//                runMessage.setIcno(resultSet.getString(2));
//                runMessage.setRm_date(resultSet.getString(3));
//                runMessage.setRm_currency(resultSet.getString(4));
//                runMessage.setRm_balance(resultSet.getDouble(5));
//                runMessage.setRm_rest(resultSet.getDouble(6));
//                runMessages.add(runMessage);
//            }
//            return runMessages;
//        }catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            DBUtils.release(connection,preparedStatement,null);
//        }
        String  sql ="select * from runmessage where icno = ? ";
        try {
            List<RunMessage> runMessageList = queryRunner.query(sql, new BeanListHandler<>(RunMessage.class), icno);
            return runMessageList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询分页数据
     * @param page 页码
     * @param pageSize 页数大小
     * @return
     */
    public List<RunMessage> findRunMessageByPage(String icno,int page, int pageSize) {
//        Connection connection = DBUtils.getConnection();
//        PreparedStatement preparedStatement = null;
//        int startIndex=(page-1)*(pageSize);
//        try {
//            preparedStatement  = connection
//                    .prepareStatement("select * from runmessage where icno = ? limit ?,? ");
//            preparedStatement.setString(1,icno);
//            preparedStatement.setInt(2,startIndex);
//            preparedStatement.setInt(3,pageSize);
//            ResultSet resultSet = preparedStatement.executeQuery();
//            ArrayList<RunMessage> runMessages = new ArrayList<>();
//            if (resultSet.next()) {
//                RunMessage runMessage = new RunMessage();
//                runMessage.setId(resultSet.getInt(1));
//                runMessage.setIcno(resultSet.getString(2));
//                runMessage.setRm_date(resultSet.getString(3));
//                runMessage.setRm_currency(resultSet.getString(4));
//                runMessage.setRm_balance(resultSet.getDouble(5));
//                runMessage.setRm_rest(resultSet.getDouble(6));
//                runMessages.add(runMessage);
//            }
//            return runMessages;
//        }catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            DBUtils.release(connection,preparedStatement,null);
//        }
        int startIndex=(page-1)*(pageSize);
        String sql = "select * from runmessage where icno = ? limit ?,? ";
        try {
            List<RunMessage> runMessageList = queryRunner.query(sql, new BeanListHandler<>(RunMessage.class), icno, startIndex, pageSize);
            return runMessageList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
